<?php

/**
 * 管理员管理
 */

namespace App\Http\Controllers\Admin;

use App\Common\Utill\Utills;
use App\Http\Controllers\Controller;
use App\Models\Admin;
use App\Service\AdministratorService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class AdministratorController extends Controller
{
    public function index()
    {
        return view('admin.administrator.index');
    }

    /**
     * 列表数据
     * @return void
     */
    public function listData(Request $request)
    {
        $num = $request->input('limit', self::ADMIN_PAGE_NUM);
        $server = new AdministratorService();
        $result = $server->getListData($request, $num);
        return Utills::resultData($result);
    }

    /**
     * 添加数据展示
     */
    public function createForm()
    {
        $getParentData = AdministratorService::getParentData();
        return view('admin.administrator.create')->with("parentData", $getParentData);
    }

    /**
     * 添加数据保存
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'username' => 'required',
            'password' => 'required'
        ], [
            'username.required' => '管理员名称不能为空',
            'password.required' => '密码不能为空',
        ]);
        if ($validator->fails()) {
            $result = ['code' => 1, 'msg' => $validator->errors()->first()];
            return response()->json($result);
        }
        $username = $request->input('username', "");
        $password = $request->input('password', "");
        $parent_id = $request->input('parent_id', 0);
        $server = new AdministratorService();
        $result = $server->create($username,$password,$parent_id);
        return Utills::resultData($result);
    }

    /**
     * 编辑展示
     * @return
     */
    public function editForm(Request $request)
    {
        $id = $request->input('id', 0);
        $data = Admin::where(['id' => $id])->first();
        $getParentData = AdministratorService::getParentData();
        return view('admin.administrator.edit')->with('data', $data)->with("parentData", $getParentData);
    }

    /**
     * 编辑保存
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'username' => 'required',
        ], [
            'username.required' => '管理员名称不能为空',
        ]);
        if ($validator->fails()) {
            $result = ['code' => 1, 'msg' => $validator->errors()->first()];
            return response()->json($result);
        }
        $username = $request->input('username', "");
        $password = $request->input('password', "");
        $id = $request->input('id', 0);
        $parent_id = $request->input('parent_id', 0);
        $server = new AdministratorService();
        $result = $server->update($id, $username,$password,$parent_id);
        return Utills::resultData($result);
    }


    /**
     * 状态修改
     * @return void
     */
    public function status(Request $request)
    {
        $validator = \Validator::make($request->all(), [
            'id' => 'required',
        ], [
            'id.required' => 'id不能为空',
        ]);
        if ($validator->fails()) {
            $result = ['code' => 404, 'msg' => $validator->errors()->first()];
            return response()->json($result);
        }
        $id = $request->input('id');
        $server = new AdministratorService();
        $result = $server->status($id);
        return response()->json($result);
    }

    /*
    * 删除
    */
    public function delete(Request $request)
    {
        if (!$request->input('id')) {
            return $this->error('删除失败');
        }
        $id = $request->input('id');
        $server = new AdministratorService();
        $result = $server->delOne($id);
        return Utills::resultData($result);
    }

    /*
     *全部删除
     */
    public function deleteAll(Request $request)
    {
        $idArr = $request->input('idList', "");
        $server = new AdministratorService();
        $result = $server->delAll($idArr);
        return Utills::resultData($result);
    }


}
